import Vue from 'vue'
import Router from 'vue-router'

const Login = resolve => require(['@/components/page/Login'], resolve)

const AppMain = resolve => require(['@/components/page/AppMain'], resolve);
const addSecDepart = resolve => require(['@/components/page/manage/addSecDepart'], resolve);
const aB = resolve => require(['@/components/page/manage/ab'], resolve);
const addSelect = resolve => require(['@/components/page/manage/addSelect'], resolve);
const autoSave = resolve => require(['@/components/page/manage/autoSave'], resolve);
const checkbox = resolve => require(['@/components/page/manage/checkbox'], resolve);
const dialogNewAndEdit = resolve => require(['@/components/page/manage/dialogNewAndEdit'], resolve);
const doctorDialog = resolve => require(['@/components/page/manage/doctorDialog'], resolve);
const dynamicValidateForm = resolve => require(['@/components/page/manage/dynamicValidateForm'], resolve);
const formCheck = resolve => require(['@/components/page/manage/formCheck'], resolve);
const jsAsync = resolve => require(['@/components/page/manage/jsAsync'], resolve);
const strRoute = resolve => require(['@/components/page/manage/strRoute'], resolve);
const upload = resolve => require(['@/components/page/manage/upload'], resolve);
const qRcode = resolve => require(['@/components/page/manage/qRcode'], resolve);
const pingzhen = resolve => require(['@/components/page/manage/pingzhen'], resolve);
const formValit = resolve => require(['@/components/page/manage/formValit'], resolve);
const selectDemo = resolve => require(['@/components/page/manage/selectDemo'], resolve);
const vuexState = resolve => require(['@/components/page/manage/vuexState'], resolve);
const viewerJs = resolve => require(['@/components/page/manage/viewerJs'], resolve);
const vuexGetters = resolve => require(['@/components/page/manage/vuexGetters'], resolve);
const vuexMutations = resolve => require(['@/components/page/manage/vuexMutations'], resolve);
const vuexAction = resolve => require(['@/components/page/manage/vuexAction'], resolve);
const comSetter = resolve => require(['@/components/page/manage/comSetter'], resolve);

Vue.use(Router)

function funProps(route) {
  console.log(route);
  let plan = route.query.plan;
  return {plan: plan}
}
const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/',
      redirect: '/addSecDepart'
    },
    {
      path: '/',
      component: AppMain,
      name: '用户管理',
      menuShow: true,
      iconCls: 'iconfont iconyonghuguanli',
      children: [
        {path: '/selectDemo', component: selectDemo, name: 'selectDemo', menuShow: true},
        {path: '/comSetter', component: comSetter, name: 'comSetter', menuShow: true},
        {path: '/vuexAction', component: vuexAction, name: 'vuexAction', menuShow: true},
        {path: '/vuexMutations', component: vuexMutations, name: 'vuexMutations', menuShow: true},
        {path: '/vuexGetters', component: vuexGetters, name: 'vuexGetters', menuShow: true},
        {path: '/viewerJs', component: viewerJs, name: 'viewerJs', menuShow: true},
        {path: '/vuexState', component: vuexState, name: 'vuexState', menuShow: true},
        {path: '/formValit', component: formValit, name: 'formValit', menuShow: true},
        {path: '/pingzhen', component: pingzhen, name: 'pingzhen', menuShow: true},
        {path: '/qRcode', component: qRcode, name: 'qRcode', menuShow: true},
        {path: '/addSecDepart', component: addSecDepart, name: 'aa', menuShow: true},
        {path: '/ab', component: aB, name: 'ab', menuShow: true},
        {path: '/addSelect', component: addSelect, name: '添加选择1', menuShow: true},
        {path: '/autoSave/:userId', component: autoSave, name: 'user', menuShow: true, props: true},
        {path: '/checkbox', component: checkbox, name: '多选框', menuShow: true},
        {path: '/dialogNewAndEdit', component: dialogNewAndEdit, name: '编辑弹框', menuShow: true},
        {path: '/doctorDialog', component: doctorDialog, name: '弹框1', menuShow: true, props: funProps},
        {path: '/dynamicValidateForm', component: dynamicValidateForm, name: '弹框', menuShow: true},
        {path: '/formCheck', component: formCheck, name: '表单校验', menuShow: true},
        {path: '/jsAsync', component: jsAsync, name: '异步', menuShow: true},
        {path: '/strRoute', component: strRoute, name: '字符串路由', menuShow: true},
        {path: '/upload', component: upload, name: '上传', menuShow: true},
      ]
    }
  ]
})

// router.beforeEach((to, from, next) => {
//   // startsWith返回一个布尔值，表示参数字符串是否在原字符串头部
//   // let arrs = localStorage.getItem('access-authType')
//   // if (arrs) {
//   //   for (let j = 0; j < router.options.routes.length; j++) {
//   //     for (let k = 0; k < arrs.length; k++) {
//   //       if (router.options.routes[j].idthat && router.options.routes[j].idthat == arrs[k]) {
//   //         router.options.routes[j].menuShow = true
//   //       }
//   //     }
//   //   }
//   // }
//   console.log(to);
//   let token = localStorage.getItem('token')
//   console.log(token);
//   let isToLogin = to.path.startsWith('/login');
//   if (token == null) {
//     localStorage.removeItem('token');
//     next({path: '/login'})
//   } else {
//     if (isToLogin) {
//       localStorage.removeItem('token')
//     } else if (to.path === '/') {
//       next({path: '/addSecDepart'})
//       return
//     }
//     next()
//   }
// })

export default router
